package com.gdsz.libbasic.view.adapter

import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil


abstract class BasePagingMultiTypeAdapter<T : Any> : PagingDataAdapter<T, BaseMultiTypeViewHolder> {
    var isHeader: Boolean = false

    constructor() : super(itemCallback())
    constructor(diffCallback: DiffUtil.ItemCallback<T>) : super(diffCallback)

    companion object {
        fun <T : Any> itemCallback(
            areItemsTheSame: (T, T) -> Boolean = { old, new -> old == new },
            areContentsTheSame: (T, T) -> Boolean = { old, new -> old == new },
            getChangePayload: (T, T) -> Any? = { _, _ -> null }
        ): DiffUtil.ItemCallback<T> {
            return object : DiffUtil.ItemCallback<T>() {
                override fun areItemsTheSame(oldItem: T, newItem: T): Boolean {
                    return areItemsTheSame(oldItem, newItem)
                }

                override fun areContentsTheSame(oldItem: T, newItem: T): Boolean {
                    return areContentsTheSame(oldItem, newItem)
                }

                override fun getChangePayload(oldItem: T, newItem: T): Any? {
                    return getChangePayload(oldItem, newItem)
                }
            }
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseMultiTypeViewHolder {
        return BaseMultiTypeViewHolder(onCreateMultiViewHolder(parent, viewType))
    }

    override fun onBindViewHolder(holder: BaseMultiTypeViewHolder, position: Int) {
            getItem(position)?.let { holder.onBindViewHolder(holder, it, position) }
            holder.binding.executePendingBindings()

    }

    abstract fun BaseMultiTypeViewHolder.onBindViewHolder(
        holder: BaseMultiTypeViewHolder,
        item: T?,
        position: Int
    )

    abstract fun onCreateMultiViewHolder(parent: ViewGroup, viewType: Int): ViewDataBinding



}